<script setup>
import {onMounted, ref} from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import * as echarts from 'echarts';

const imageUrl = ref('')

const handleAvatarSuccess = (
    response
) => {
  if (response.code === 1) {
    imageUrl.value = "/api/uploads/" + response.data
  } else {
    ElMessage.error('上传失败')
  }
}

const beforeAvatarUpload = (rawFile) => {
  const supportFileType = ['jpg', 'jpeg', 'png', 'gif']
  const imageTypePattern = new RegExp(`^image/(${supportFileType.join('|')})$`)
  if (!imageTypePattern.test(rawFile.type)) {
    ElMessage.error(`支持的文件类型: ${supportFileType.join(', ')}`)
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('最大文件大小：2MB')
    return false
  }
  return true
}

onMounted(() => {
  const ec = echarts.init(document.querySelector('#ec'))

  const option = {
    title: {
      text: ' 图'
    },
    radar: {
      // shape: 'circle',
      indicator: [
        { name: 'A', max: 1 },
        { name: 'B', max: 1 },
        { name: 'C', max: 1 },
        { name: 'D', max: 1 },
        { name: 'E', max: 1 },
        { name: 'F', max: 1 }
      ]
    },
    series: [
      {
        name: 'Budget vs spending',
        type: 'radar',
        data: [
          {
            value: [0.8, 0.3, 0.6, 1.0, 0.9, 0.1],
          }
        ]
      }
    ]
  };

  ec.setOption(option);
})

</script>

<template>
  <el-upload
      class="avatar-uploader"
      action="/api/uploads"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  <div id="ec" style="width: 600px; height: 600px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%)"></div>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>